package org.osflash.signals;

import asunit.asserts.*;
import asunit.framework.IAsync;

class SignalDispatchNonEventTest {

	@:meta(Inject())
	public var async : IAsync;
	public var completed : Signal;
	@:meta(Before())
	public function setUp() : Void {
		completed = new Signal(Date);
	}

	@:meta(After())
	public function tearDown() : Void {
		completed.removeAll();
		completed = null;
	}

	/**
	 * Captures bug where dispatching 0 was considered null.
	 */
	@:meta(Test())
	public function dispatch_zero_should_call_listener_with_zero() : Void {
		completed = new Signal(Number);
		completed.add(async.add(onZero, 10));
		completed.dispatch(0);
	}

	function onZero(num : Float) : Void {
		assertEquals(0, num);
	}

	//////
	@:meta(Test())
	public function dispatch_2_zeroes_should_call_listener_with_2_zeroes() : Void {
		completed = new Signal(Number, Number);
		completed.add(async.add(onZeroZero, 10));
		completed.dispatch(0, 0);
	}

	function onZeroZero(a : Dynamic, b : Dynamic) : Void {
		assertEquals(0, a);
		assertEquals(0, b);
	}

	//////
	@:meta(Test())
	public function dispatch_null_should_call_listener_with_null() : Void {
		completed.addOnce(async.add(checkNullDate, 10));
		completed.dispatch(null);
	}

	function checkNullDate(date : Date) : Void {
		assertNull(date);
	}

	//////
	@:meta(Test())
	public function dispatch_null_through_int_Signal_should_be_autoconverted_to_zero() : Void {
		completed = new Signal(int);
		completed.addOnce(async.add(checkNullConvertedToZero, 10));
		completed.dispatch(null);
	}

	function checkNullConvertedToZero(intValue : Int) : Void {
		assertEquals("null was converted to 0", 0, intValue);
	}

}

